package receiver;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.concerneyes.CEApp;
import com.concerneyes.MainActivity;
import com.concerneyes.R;
import com.concerneyes.SplashActivity;
import com.concerneyes.health.HealthActivity;
import com.concerneyes.mine.TabActivity.MyInfo;
import com.concerneyes.mine.TabActivity.MyServiceCentre;
import com.concerneyes.mine.TabActivity.OrderView.MyOrderActivity;
import com.concerneyes.mine.TabActivity.OrderView.MyOrderDetail;

import cn.jpush.android.api.JPushInterface;
import common.WebViewActivity;
import http.JsonMap;
import http.Url;

/**
 * 自定义接收器
 * <p>
 * 如果不定义这个 Receiver，则： 1) 默认用户会打开主界面 2) 接收不到自定义消息
 */
public class JPushReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";

    //id pushType
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.e(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
            context = CEApp.getInstance();
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            JsonMap json = JsonMap.parseJson(message);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            String message = bundle.getString(JPushInterface.EXTRA_EXTRA);
            JsonMap json = JsonMap.parseJson(message);
            Log.e(TAG, "[MyReceiver] 接收到推送下来的自定义消息: 打开界面" + json);
            String pushType = json.getString("pushType");
            String id = json.getString("id");

            if (pushType.equals(CEApp.PUSH_ORDER)) {

                Bundle bundle1 = new Bundle();
                bundle1.putString("orderId", id);
                Intent intent1 = new Intent(context, MyOrderDetail.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent1.putExtras(bundle1);
                context.startActivity(intent1);
            } else if (pushType.equals(CEApp.PUSH_ACTIVITY)) {
                Bundle bundle1 = new Bundle();
                bundle1.putString("activityUrl", Url.ActivityDetail + id);
                String title = context.getResources().getString(R.string.extension_detail);
                bundle1.putString("title",title);
                Intent intent1 = new Intent(context, WebViewActivity.class);
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent1.putExtras(bundle1);
                context.startActivity(intent1);
            }

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {

        }
    }
}
